/**
 * User: Célia Calçada
 * Date: 01-07-2013
 * Ext app controller (and i18n initializer)
 */

//<debug>
/*
Ext.Loader.setPath({
    'Ext': 'touch/src',
    'Mobile-app': 'app'
});
*/
//</debug>

//load language settings when the document's ready (i18n)
Ext.onReady(function(){

    //get language from cookies
    var LANG = Ext.util.Cookies.get("i18next");

    $.i18n.init({
        lng            : LANG ? LANG : 'pt', //check for a default language
        fallbackLng    : 'en',
        resGetPath     : 'lang/__lng__.json',
        useLocalStorage: false,
        ns             : { namespaces: ['general', 'error', 'login'], defaultNs: 'general'}
    }, function () {

        //check if the client's browser if from a desktop
        if(Ext.os.is.Desktop && confirm($.t('login.visualize the optimized app for desktop environments'))){
            //go the the general app
            window.location = 'https://www.moovefleet.com/';
        }

        //check if browser has the webkit layout
        if (!Ext.browser.is.webkit){
            //inform user that the browser is not ready to be used with this application
            alert($.t('login.browser without webkit warning'));
            //go the the general app
            window.location = 'https://www.moovefleet.com/';
        }

        Ext.application({
            glossOnIcon: false,
            name: 'Mobile-app',

            requires: [
                'Ext.data.Store',
                'Ext.MessageBox',
                'Ext.Map',
                'Ext.plugin.google.Traffic',
                'Ext.plugin.google.Tracker',
                'Ext.SegmentedButton',
                'Ext.dataview.List',
                'Ext.field.Search',
                'Ext.field.Select',
                'Ext.field.Number'
            ],

            views: [
                'Main',
                'Login',
                'Mobile',
                'MobileDetail',
                'Map',
                'Poi',
                'PoiDetail',
                'Driver',
                'DriverDetail',
                'Trailer',
                'TrailerDetail',
                'AddPoi',
                'EditPoi',
                'WhatsAround'
            ],

            controllers: [
                'FilterList',
                'Login',
                'Mobile',
                'Poi',
                'Trailer',
                'Driver',
                'Map'
            ],

            models: [
                'Mobile',
                'Driver',
                'Poi',
                'Trailer',
                'Event',
                'WhatsAround',
                'PoiCategory'
            ],

            /***
             * It's triggered when the APP is loaded
             */
            launch: function() {

                // Destroy the #appLoadingIndicator element
                Ext.fly('appLoadingIndicator').destroy();

                // Initialize the main view
                Ext.Viewport.add([
                    { xtype: 'loginview' },
                    { xtype: 'mainview' }
                ]);
            }
        });

    });

});
